//
//  delegListViewController.m
//  gpos_app
//
//  Created by apple on 2018/10/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "delegListViewController.h"
#import "Masonry.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "WKProgressHUD.h"
#import "XMRegisterViewController.h"
#import "delegViewCell.h"
#import "storageModel.h"

@interface delegListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)WKProgressHUD * hud;
@property (nonatomic,strong)UIView * topView;
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)NSMutableArray * modelArr;

@end

@implementation delegListViewController

//改变状态栏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
//    self.navigationController.navigationBar.hidden = NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    self.title = @"代理详情";
    [self dataLoad];
}

- (void)dataLoad{
    __weak typeof(self) weakSelf = self;
    self.modelArr = [[NSMutableArray alloc] init];
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{
                              @"userId" : @(user),
                              };
    //    NSLog(@"oj---%@",params);
    [GPosAPI requestSaleDetailWithParams:params andCallback:^(id obj) {
        NSLog(@"储存列表---%@",obj);
        
        if (obj==nil) {
            weakSelf.hud=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
            
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {

                NSArray * arr = [[NSArray alloc] init];
                arr = [obj objectForKey:@"data"];
                for (NSDictionary * dic in arr) {
                    storageModel *model = [[storageModel alloc] init];
                    NSDecimalNumber * number1 = [dic objectForKey:@"reward"];
                    model.reward = [NSString stringWithFormat:@"%@",@(number1.floatValue)];
                    model.number = [[dic objectForKey:@"number"] integerValue];
                    [weakSelf.modelArr addObject:model];
                }
                [weakSelf.tableView reloadData];
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402){
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else{
                
                weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
            }
            
            
        }
        
        [weakSelf UIViewloadData];
    }];
    
}

- (void)UIViewloadData{
    self.topView = [[UIView alloc] init];
    self.topView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(kNavbarHeight + 10);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(@40);
    }];
    
//    UILabel * oneLaebl = [[UILabel alloc] init];
//    oneLaebl.backgroundColor = [UIColor lightGrayColor];
//    oneLaebl.text = @"销售层级";
//    oneLaebl.textAlignment = NSTextAlignmentCenter;
//    oneLaebl.font = [UIFont systemFontOfSize:14];
//    [self.topView addSubview:oneLaebl];
    
    UILabel * twoLaebl = [[UILabel alloc] init];
    twoLaebl.backgroundColor = [UIColor lightGrayColor];
    twoLaebl.text = @"台数";
    twoLaebl.textAlignment = NSTextAlignmentCenter;
    twoLaebl.font = [UIFont systemFontOfSize:14];
    [self.topView addSubview:twoLaebl];
    
    UILabel * threeLaebl = [[UILabel alloc] init];
    threeLaebl.backgroundColor = [UIColor lightGrayColor];
    threeLaebl.text = @"金额";
    threeLaebl.textAlignment = NSTextAlignmentCenter;
    threeLaebl.font = [UIFont systemFontOfSize:14];
    [self.topView addSubview:threeLaebl];
    [@[ twoLaebl, threeLaebl] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:1 tailSpacing:1];
    
    [@[twoLaebl, threeLaebl] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView).offset(1);
        make.bottom.equalTo(self.topView).with.offset(-1);
        
    }];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).with.offset(0);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.bottom.equalTo(self.view).with.offset(-kBottomSafeHeight);
    }];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArr.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//rankingCell
    
    delegViewCell * cell = [[delegViewCell alloc] init];
    
    cell = [tableView dequeueReusableCellWithIdentifier:@"delegCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"delegViewCell" owner:self options:nil] firstObject];
    }
//    NSArray * arr = @[@"1级",@"2级",@"3级"];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld",[self.modelArr[indexPath.row] number]];
    cell.rewardLabel.text = [self.modelArr[indexPath.row] reward];
//    cell.cengjiLabel.text = arr[indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

@end
